package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;

public class Login extends JPanel {
	private JTextField userNameInput;
	private JPasswordField passwordInput;
	private JButton btnContinue;
	private JButton btnShutDown;
	private JMenuItem mntmAdminMenu;
	private JMenuItem mntmUserMenu;

	
	public Login() {
		setBackground(UIManager.getColor("Button.background"));
		setLayout(null);

		JLabel lblLogin = new JLabel("Login");
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblLogin.setBounds(206, 32, 39, 17);
		add(lblLogin);

		btnContinue = new JButton("Continue");
		btnContinue.setToolTipText("");
		btnContinue.setBounds(270, 223, 96, 23);
		add(btnContinue);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(btnContinue, popupMenu);
		
		mntmAdminMenu = new JMenuItem("Admin menu");
		popupMenu.add(mntmAdminMenu);
		
		mntmUserMenu = new JMenuItem("User menu");
		popupMenu.add(mntmUserMenu);

		btnShutDown = new JButton("Shut down");
		btnShutDown.setToolTipText("Closes BCBS");
		btnShutDown.setBounds(75, 223, 96, 23);
		add(btnShutDown);

		JLabel lblNewLabel = new JLabel("Username: ");
		lblNewLabel.setBounds(75, 89, 70, 16);
		add(lblNewLabel);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(75, 139, 72, 14);
		add(lblPassword);

		userNameInput = new JTextField();
		userNameInput.setColumns(10);
		userNameInput.setBounds(151, 89, 147, 23);
		add(userNameInput);

		passwordInput = new JPasswordField();
		passwordInput.setBounds(151, 136, 147, 23);
		add(passwordInput);
		
		JLabel lblHjreklikForAdmin = new JLabel("H\u00F8jreklik for admin");
		lblHjreklikForAdmin.setBounds(270, 254, 123, 16);
		add(lblHjreklikForAdmin);
	}

	public void addActionListener(ActionListener l) {
		btnContinue.addActionListener(l);
		btnShutDown.addActionListener(l);
		mntmAdminMenu.addActionListener(l);
		mntmUserMenu.addActionListener(l);

	}

	public JTextField getUserNameInput() {
		return userNameInput;
	}

	public JPasswordField getPasswordInput() {
		return passwordInput;
	}

	public JButton getBtnContinue() {
		return btnContinue;
	}

	public JButton getBtnShutDown() {
		return btnShutDown;
	}
	public JMenuItem getMntmAdminMenu() {
		return mntmAdminMenu;
	}

	public JMenuItem getMntmUserMenu() {
		return mntmUserMenu;
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
